package hn.sigit.logic.viewer;

import hn.sigit.logic.workflow.transaction.impl.DataEntryRRRHelperLogic;
import hn.sigit.logic.workflow.transaction.impl.ParcelResponsibility;
import hn.sigit.logic.workflow.transaction.impl.ParcelRestriction;
import hn.sigit.logic.workflow.transaction.impl.ParcelRight;
import hn.sigit.model.hnd.cadastre.HND_Property;
import hn.sigit.model.ladm.administrative.LA_RRR;
import hn.sigit.model.ladm.administrative.LA_Responsibility;
import hn.sigit.model.ladm.administrative.LA_Restriction;
import hn.sigit.model.ladm.administrative.LA_Right;
import hn.sigit.model.ladm.external.ExtParty;
import hn.sigit.util.ShareValue;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class InteractiveViewerRRRHelper implements Serializable {
	private static final long serialVersionUID = 7630006624830334101L;

	private InteractiveViewerHelper interactiveViewerHelper;
	private List<ParcelRight> parcelRightsList;
	private List<ParcelRestriction> parcelRestrictionsList;
	private List<ParcelResponsibility> parcelResponsibilitiesList;
	
	private HND_Property property;
	private Set<LA_RRR> rrrSet;
	
	public InteractiveViewerRRRHelper(InteractiveViewerHelper ivh) {
		interactiveViewerHelper = ivh;
	}

	
	public HND_Property getProperty() {
		if (property == null)
			property = (HND_Property)interactiveViewerHelper.getSelectedProperty();

		return property;
	}
	public void setProperty(HND_Property property) {
		this.property = property;
	}

	public Set<LA_RRR> getRrrSet() {
		if (rrrSet == null) {
			if (getProperty() != null)
				rrrSet = getProperty().getRrr();
		}
		return rrrSet;
	}
	public void setRrrSet(Set<LA_RRR> rrrSet) {
		this.rrrSet = rrrSet;
	}

	public List<ParcelRight> getParcelRightsList() {
		if (parcelRightsList == null) {
			parcelRightsList = new ArrayList<ParcelRight>();
			
			if (getRrrSet() != null) {
				ParcelRight pr;
				for (LA_RRR rrr : getRrrSet()) {
					pr = null;
					if (rrr instanceof LA_Right) {
						LA_Right right = (LA_Right)rrr;
						ExtParty extParty = right.getParty().getExtParty();
						pr = new ParcelRight(0, extParty, right.getType(),
								new ShareValue(right.getShare()), right);
					}
					
					if (pr != null)
						parcelRightsList.add(pr);
				}
			}
		}
		
		return parcelRightsList;
	}
	public void setParcelRightsList(List<ParcelRight> parcelRightsList) {
		this.parcelRightsList = parcelRightsList;
	}
	
	public ShareValue getRightsSumShares() {
		return DataEntryRRRHelperLogic.getSumShares(parcelRightsList);
	}

	
	public List<ParcelRestriction> getParcelRestrictionsList() {
		if (parcelRestrictionsList == null) {
			parcelRestrictionsList = new ArrayList<ParcelRestriction>();
			
			if (getRrrSet() != null) {
				ParcelRestriction pr;
				for (LA_RRR rrr : getRrrSet()) {
					pr = null;
					if (rrr instanceof LA_Restriction) {
						LA_Restriction restriction = (LA_Restriction)rrr;
						ExtParty extParty = restriction.getParty().getExtParty();
						pr = new ParcelRestriction(0, extParty, restriction.getType(),
								new ShareValue(restriction.getShare()), restriction);
					}
					
					if (pr != null)
						parcelRestrictionsList.add(pr);
				}
			}
		}

		return parcelRestrictionsList;
	}
	public void setParcelRestrictionsList(
			List<ParcelRestriction> parcelRestrictionsList) {
		this.parcelRestrictionsList = parcelRestrictionsList;
	}

	public List<ParcelResponsibility> getParcelResponsibilitiesList() {
		if (parcelResponsibilitiesList == null) {
			parcelResponsibilitiesList = new ArrayList<ParcelResponsibility>();

			if (getRrrSet() != null) {
				ParcelResponsibility pr;
				for (LA_RRR rrr : getRrrSet()) {
					pr = null;
					if (rrr instanceof LA_Responsibility) {
						LA_Responsibility responsibility = (LA_Responsibility)rrr;
						ExtParty extParty = responsibility.getParty().getExtParty();
						pr = new ParcelResponsibility(0, extParty, responsibility.getType(),
								new ShareValue(responsibility.getShare()), responsibility);
					}
					
					if (pr != null)
						parcelResponsibilitiesList.add(pr);
				}
			}
		}
		
		return parcelResponsibilitiesList;
	}
	public void setParcelResponsibilitiesList(
			List<ParcelResponsibility> parcelResponsibilitiesList) {
		this.parcelResponsibilitiesList = parcelResponsibilitiesList;
	}
	
	
}
